using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class AggressiveWeapons : Weapon
{
    private readonly List<IDamageable> DetectedDamageables = new();
    private readonly List<IKnockBackable> DetectedKnockBackables = new();
    protected SO_AggressiveWeaponData _aggressiveWeaponData;

    protected override void Awake()
    {
        base.Awake();

        if (weaponData.GetType() == typeof(SO_AggressiveWeaponData))
            _aggressiveWeaponData = (SO_AggressiveWeaponData)weaponData;
        else
            Debug.LogError("Wrong Data for Weapon!");
    }

    public override void AnimationActionTrigger()
    {
        base.AnimationActionTrigger();

        CheckMeleeAttack();
    }

    private void CheckMeleeAttack()
    {
        var details = _aggressiveWeaponData.AttackDetails[_attackCounter];
        foreach (var weapon in DetectedDamageables.ToList())
        {
            //weapon.Damage(details._damageAcount);
        }

        foreach (var item in DetectedKnockBackables.ToList())
            item.KnockBack(details._knockBackAngle, details._knockBackStrength, _core.Movement.FacingDirection);
    }

    public void AddToDetected(Collider2D collision)
    {
        var damageable = collision.GetComponent<IDamageable>();
        if (damageable != null) DetectedDamageables.Add(damageable);

        var knockBackable = collision.GetComponent<IKnockBackable>();
        if (knockBackable != null) DetectedKnockBackables.Add(knockBackable);
    }

    public void RemoveFromDetected(Collider2D collision)
    {
        var damageable = collision.GetComponent<IDamageable>();

        if (damageable != null) DetectedDamageables.Remove(damageable);

        var knockBackable = collision.GetComponent<IKnockBackable>();
        if (knockBackable != null) DetectedKnockBackables.Remove(knockBackable);
    }
}